﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;

namespace ConnectLib
{
    public class Globals
    {
        private static string m_LogPath = string.Empty;

        public static string LogPath
        {
            get { return m_LogPath; }
            set { m_LogPath = value; }
        }

        public static string encrypt(string pass)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Trim(), "SHA1");
        }

        public static void WriteLog(string fileName, string strContent)
        {
            try
            {
                string path = string.Empty;
                if (!string.IsNullOrEmpty(m_LogPath))
                    path = m_LogPath;
                else
                    path = System.Web.HttpContext.Current.Server.MapPath("~\\LogFiles");
                if (!Directory.Exists(path))
                    Directory.CreateDirectory(path);

                string filenameLog = path + "\\" + fileName;

                try
                {
                    FileInfo fi = new FileInfo(filenameLog);
                    if (fi.Length > 52428800)    //50MB
                    {
                        File.Move(filenameLog, path + "\\" + fileName.Replace(".log", "_" + DateTime.UtcNow.ToString("yyyyMMddHHmmssfff") + ".log"));
                    }
                }
                catch { }

                StreamWriter writer = new StreamWriter(filenameLog, true);
                writer.WriteLine(strContent);
                writer.Close();
            }
            catch
            {

            }
        }
    }

}
